package Client.GUI.Popups;

import Client.Net.*;
import java.rmi.*;
import java.awt.Color;
import Client.*;
import Library.ErrorCodes;
import Library.Events.TimeEvent;
import Library.Game.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

/**
 *
 */
public class popDefend extends AbstractPopup
{
    protected LandInfo m_AttackingLand;
    protected LandInfo m_DefendingLand;
    
    /**
     * Creates new form popDefend
     */
    public popDefend(ICommunication communication, IPopupContainer container, LandInfo attLand, LandInfo defLand, int amount, int recDefAmount) throws RemoteException
    {
	super(communication, container);
        m_Spinmax = recDefAmount;
        m_Spindef = m_Spinmax;
	initComponents();
        m_AttackingLand = attLand;
        m_DefendingLand = defLand;
        
	
	try
	{
	    Color attClr = PlayerColor.getColor(m_AttackingLand.getOwner().getColor());
	    Color defClr = PlayerColor.getColor(m_DefendingLand.getOwner().getColor());
	    lblAttackerWho.setForeground(attClr);
	    lblAttackerLand.setForeground(attClr);
	    lblAttackerArmies.setForeground(attClr);
	    lblAttackerArmiesAttacking.setForeground(attClr);
	    lblDefenderWho.setForeground(defClr);
	    lblDefenderLand.setForeground(defClr);
	    lblDefenderArmies.setForeground(defClr);
	    lblTimeRemaining.setForeground(defClr);
	    lblTimeRemainingText.setForeground(defClr);
	}
	catch (RemoteException ex)
	{
	    ex.printStackTrace();
	}
        
	lblAttackerWho.setText("Attacker " + m_AttackingLand.m_Player.getName());
	lblAttackerLand.setText(m_AttackingLand.getName());
	lblAttackerArmies.setText(m_AttackingLand.getAmountOfArmies() + " soldiers stationed");
        lblDefenderWho.setText("Defender " + m_DefendingLand.m_Player.getName());
	lblDefenderLand.setText(m_DefendingLand.getName());
	lblDefenderArmies.setText(m_DefendingLand.getAmountOfArmies() + " soldiers stationed");
	numAantal.setValue(new Integer(recDefAmount));
	lblAttackerArmiesAttacking.setText(amount + " soldiers attacking");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAttack = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        numAantal = new javax.swing.JSpinner();
        jPanel1 = new javax.swing.JPanel();
        lblAttackerLand = new javax.swing.JLabel();
        lblAttackerArmies = new javax.swing.JLabel();
        lblAttackerWho = new javax.swing.JLabel();
        lblAttackerArmiesAttacking = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        lblDefenderLand = new javax.swing.JLabel();
        lblDefenderArmies = new javax.swing.JLabel();
        lblDefenderWho = new javax.swing.JLabel();
        lblTimeRemainingText = new javax.swing.JLabel();
        lblTimeRemaining = new javax.swing.JLabel();

        btnAttack.setText("Defend");
        btnAttack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAttackActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("You are Under attack!!");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("With how many soldiers do you want to defend ?");

        numAantal.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        numAantal.setModel(new javax.swing.SpinnerNumberModel(m_Spindef, 1, m_Spinmax, 1));

        lblAttackerLand.setText("Land");

        lblAttackerArmies.setText("Armies");

        lblAttackerWho.setText("Who");

        lblAttackerArmiesAttacking.setText("<Amount>");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAttackerWho)
                    .addComponent(lblAttackerLand)
                    .addComponent(lblAttackerArmies)
                    .addComponent(lblAttackerArmiesAttacking))
                .addContainerGap(127, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(lblAttackerWho)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAttackerLand)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAttackerArmies)
                .addGap(18, 18, 18)
                .addComponent(lblAttackerArmiesAttacking)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        lblDefenderLand.setText("Land");

        lblDefenderArmies.setText("Armies");

        lblDefenderWho.setText("Who");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDefenderWho)
                    .addComponent(lblDefenderLand)
                    .addComponent(lblDefenderArmies))
                .addContainerGap(126, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(lblDefenderWho)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDefenderLand)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDefenderArmies)
                .addContainerGap(50, Short.MAX_VALUE))
        );

        lblTimeRemainingText.setText("Time Remaining:");

        lblTimeRemaining.setFont(new java.awt.Font("Tahoma", 0, 18));
        lblTimeRemaining.setText("0 seconds");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(numAantal, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(140, 140, 140)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblTimeRemaining, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                                    .addComponent(lblTimeRemainingText, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(22, 22, 22))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAttack)
                        .addGap(141, 141, 141))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTimeRemainingText)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblTimeRemaining, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(numAantal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE))
                .addGap(29, 29, 29)
                .addComponent(btnAttack)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAttackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAttackActionPerformed
	try
	{
	    int result = m_Communication.getService().defend((Integer) this.numAantal.getValue());
	    if (result != ErrorCodes.ERROR_NOERROR)
		JOptionPane.showMessageDialog(this, ErrorCodes.getMessage(result), "Error Sending Defend-Amount", JOptionPane.ERROR_MESSAGE);
	}
	catch (RemoteException ex)
	{
	   ex.printStackTrace();
	}
	catch (Exception ex)
	{
	   ex.printStackTrace();
	}
    }//GEN-LAST:event_btnAttackActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAttack;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblAttackerArmies;
    private javax.swing.JLabel lblAttackerArmiesAttacking;
    private javax.swing.JLabel lblAttackerLand;
    private javax.swing.JLabel lblAttackerWho;
    private javax.swing.JLabel lblDefenderArmies;
    private javax.swing.JLabel lblDefenderLand;
    private javax.swing.JLabel lblDefenderWho;
    private javax.swing.JLabel lblTimeRemaining;
    private javax.swing.JLabel lblTimeRemainingText;
    private javax.swing.JSpinner numAantal;
    // End of variables declaration//GEN-END:variables
    protected int m_Spinmax;
    protected int m_Spindef;

    @Override
    protected void register()
    {
	m_Communication.getService().onDefendTimerEvent.register(onDefendTimerEvent);
    }
    
    @Override
    protected void unregister()
    {
	m_Communication.getService().onDefendTimerEvent.unregister(onDefendTimerEvent);
    }
    
    /**
     * Event om te laten zien hoeveel tijd de verdediger over heeft om te besluiten met hoeveel legers hij verdedigt.
     * @param time resterende tijd
     * @param totaltime totale tijd
     * @throws RemoteException
     * @throws Exception 
     */
    protected ActionListener onDefendTimerEvent = new ActionListener()
    {
	@Override
	public void actionPerformed(ActionEvent e)
	{
	    try
	    {
		TimeEvent ev = (TimeEvent)e;
		int time = ev.getTime();
		int totaltime = ev.getTotalTime();
		System.out.println("onDefendTimerEvent: " + time + " " + totaltime);
		lblTimeRemaining.setText(time + " seconds left");
	    }
	    catch (Exception ex)
	    {
		ex.printStackTrace();
	    }
	}
    };
}
